<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_advertising\service;
use cmf\controller\PluginBaseController;
use think\Db;

class AdsService extends PluginBaseController
{

	/**
	 * 获取广告数组
	 * 使用方法：
	 *     use plugins\qbt_advertising\service\AdsService;
	 *     AdsService::getAd('topbanner',10);
	 * @param string $logo
	 * @param int $num
	 * @return array
	 */
    static public function getAd($logo,$num=0)
    {
    	$lists = Db::name('ad_position')->where(['ad_logo'=>$logo])->find();

    	$start=strtotime(date('Y-m-d H:i:s',time()));
    	$where['position_id'] = ['eq',$lists['id']];
    	$where['start_time'] = ['<= time', $start];
    	$where['end_time'] = ['>= time',$start];
    	$where['enabled'] = ['eq',1];
    	if($num){
    		$list = Db::name('ad')->where($where)->order('id desc')->limit($num)->select();
    	}else{
    		$list = Db::name('ad')->where($where)->order('id desc')->select();
    	}
		

        return $list;
    }

}
